﻿CREATE PROCEDURE [dbo].[Stores_InsertFromTempStores]
	@CountryId smallint, 
	@RangeReviewId smallint
AS
	INSERT INTO Stores(CountryId, Code, Name, Region, Subregion, City, State, SEL, ShoppingOccasion, Size, Address, Phone, Fax, Format, PBL, Mail, Contact, SiteLocation, ShopType, Layout, OpeningHours, Attribute1, Attribute2, SELCustomers, SELLocation)
	SELECT distinct CountryId, Code, Name, Region, Subregion, City, State, SEL, ShoppingOccasion, Size, Address, Phone, Fax, Format, PBL, Mail, Contact, SiteLocation, ShopType, Layout, OpeningHours, Attribute1, Attribute2, SELCustomers, SELLocation
	FROM TempStores temp
	WHERE temp.CountryId = @CountryId and
	NOT EXISTS
		(select * 
		 from Stores st
		 where CountryId = @CountryId and
			   st.Code = temp.Code)
			   				   			   
	INSERT INTO RangeReviewStores(RangeReviewId, StoreId, IsExcluded, SKUCoverage, SKUPenetration, UseToCreateArticleMaster, IsSameStore)
	SELECT @RangeReviewId, st.Id, 1, 1, 1, 0, 0
	FROM Stores st
	WHERE st.CountryId = @CountryId
	